//
//  VisitorView.swift
//  DSWeiBo
//
//  Created by Frank Wang on 7/6/16.
//  Copyright © 2016 Frank Wang. All rights reserved.
//

import UIKit

//swift 定义协议,必须遵守NSObjectProtocol这个协议

protocol VisitorViewDelegate: NSObjectProtocol{
        
        //登录回调
        func loginBtnClick()
        //注册回调
        func registerBtnClick()
}

class VisitorView: UIView {

        
        //定义一个对象保存代理属性，一定要加上weak，防止循环引用
        weak var delegate : VisitorViewDelegate?
        
        
        /**
         设置为登录界面
         
         - parameter isHome:    是否是首页
         - parameter imageName: 需要展示的图标
         - parameter message:   需要展示的文本内容
         */
        func setupVisitorInfo(isHome:Bool,imageName:String,message:String){
                //如果不是首页就隐藏键盘
                iconView.hidden = !isHome
                //修改中间图标
                homeIcon.image = UIImage(named:imageName)
                //修改文本
                messageLabel.text = message;
                
                if isHome {
                        self .startAnimation()
                }
                
        }
        
        
        func loginBtnClick(){
               delegate?.loginBtnClick()
        }
        //注册回调
        func registerBtnClick(){
                delegate?.registerBtnClick()
        }
        
       override init(frame: CGRect) {
                super.init(frame: frame)
                //1.添加子控件
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginbtn)
        addSubview(registerbtn)
                //2.布局子控件(autoLayout)
        //设置背景
        iconView.xmg_AlignInner(type:XMG_AlignType.Center, referView: self, size: nil)
        
        //设置图标
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        
        //标题
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        
        //哪个控件的   什么属性   等于另一个控件  的 什么属性  乘以多少  在加上多少。
        let widthCons = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
        
        addConstraint(widthCons)
        
        //设置按钮
        //拉伸图片可以点击图片，然后进入 show sliciing
        registerbtn.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSizeMake(100, 30),offset: CGPointMake(0, 20))
        
        loginbtn.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSizeMake(100, 30), offset: CGPointMake(0, 20))
        
        //设置蒙板
//        maskBGView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        maskBGView.xmg_Fill(self)
        }
        //swift推荐我们自定义一个控件，要么纯代码，要么就用XIB
        required init?(coder aDecoder: NSCoder) {
                //如果通过XIB创建该类就会奔溃
                fatalError("init(coder) has not implement")
        }

        //MARK ： 内部调用方法
        private func startAnimation(){
                let ani = CABasicAnimation(keyPath: "transform.rotation")
                ani.toValue = 2 * M_PI
                ani.duration = 20
                ani.repeatCount = MAXFLOAT
                
                //该属性默认为YES，代表动画只要一执行完毕就移除
                ani.removedOnCompletion = false
                iconView.layer.addAnimation(ani, forKey: nil)
        }
        
        //MARK ： － 懒加载控件
        /// 转盘
        private lazy var iconView : UIImageView = {
           
                let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
                return iv
        }()
        /// 图标
        private lazy var homeIcon : UIImageView = {
           
                let iv = UIImageView(image : UIImage(named: "visitordiscover_feed_image_house"))
                return iv
                
        }()
        /// 文本
        private lazy var messageLabel : UILabel = {
                let meaasgeLabel = UILabel()
                meaasgeLabel.text = "快来啊注册吧，快来啊注册吧"
                meaasgeLabel.numberOfLines = 0;
                meaasgeLabel.textColor = UIColor.lightGrayColor()
                return meaasgeLabel
        }()
        
        private lazy var loginbtn:UIButton = {
           let btn = UIButton()
                btn.setTitle("登录", forState: UIControlState.Normal)
                btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
                btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
                
                btn.addTarget(self, action: #selector(VisitorView.loginBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
                return btn
                
        }()
        
        private lazy var registerbtn:UIButton = {
                let btn = UIButton()
                btn.setTitle("注册", forState: UIControlState.Normal)
                btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
                btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
                btn.addTarget(self, action: #selector(BaseTableViewController.registerBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
                return btn
                
        }()
        
        private lazy var maskBGView : UIImageView = {
                let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
                return iv
        }()
        
}
